// 倒计时模块

class Timer {
    countdown (end,update,handle) {
        const now = new Date().getTime();
        const self = this;
        if (now-end) {

            handle.call(self);

        } else {

            let left_time = end - now;
            const px_day = 1000*60*60*24;
            const px_hour = 1000*60*60; 
            const px_mins = 1000*60; 
            const px_secs = 1000;
            let d = Math.floor(left_time / px_day);
            let h = Math.floor((left_time - d*px_day) / px_hour);
            let m = Math.floor((left_time - d*px_day - h*px_hour) / px_mins);
            let s = Math.floor((left_time - d*px_day - h*px_hour - m*px_mins) / px_secs);
            let r = [];

            if (d > 0) {
                r.push(`<em>${d}</em>天`);    
            }

            if (r.length || (h>0)) {
                r.push(`<em>${h}</em>时`);    
            }

            if (r.length || (m>0)) {
                r.push(`<em>${m}</em>分`);    
            }

            if (r.length || (s>0)) {
                r.push(`<em>${s}</em>秒`);    
            }

            self.left_time = r.join('');    
            update.call(self,r.join(''));

            setTimeout(() => {
                self.countdown(end,update,handle);
            }, 1000);
             
        }
    }
}

module.exports = {
    Timer
}